<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring MVC控制器详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            position: relative;
        }
        .code-block .lang {
            position: absolute;
            top: 8px;
            right: 12px;
            color: #9e9e9e;
            font-size: 0.8rem;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
        }
        .diagram-container {
            background-color: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-4xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">Spring MVC控制器深度解析</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">探索@Controller与@RestController的核心差异与应用场景</p>
            <div class="flex justify-center space-x-4">
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">Spring MVC</span>
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">RESTful</span>
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">Web开发</span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-4xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-lg text-gray-700 leading-relaxed mb-6">
                在Spring MVC框架中，<code class="bg-gray-100 px-2 py-1 rounded">@Controller</code>和<code class="bg-gray-100 px-2 py-1 rounded">@RestController</code>是两个核心注解，它们为构建Web应用程序提供了强大的支持。虽然它们都用于处理HTTP请求，但在设计理念和返回值处理上有着显著差异。
            </p>
            <div class="flex items-center text-blue-600 mb-8">
                <i class="fas fa-lightbulb mr-2"></i>
                <span>关键洞察：主要区别在于返回值的处理方式，@Controller返回视图而@RestController返回数据对象</span>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 font-serif border-b pb-2">对比分析</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <!-- @Controller Card -->
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-4">
                        <div class="bg-blue-100 text-blue-600 p-3 rounded-full mr-4">
                            <i class="fas fa-file-alt text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold">@Controller</h3>
                    </div>
                    <p class="text-gray-700 mb-4">
                        <code>@Controller</code>用于标识一个类为Spring MVC的控制器组件，处理用户请求并返回视图响应。通常配合视图解析器返回HTML页面或模板。
                    </p>
                    <div class="code-block p-4 mb-4 text-sm overflow-x-auto">
                        <div class="lang">Java</div>
                        <pre><code>@Controller
public class ExampleController {
    @RequestMapping("/example")
    public String handleRequest() {
        // 处理请求并返回视图名称
        return "view";
    }
}</code></pre>
                    </div>
                    <div class="grid grid-cols-2 gap-2 mb-4">
                        <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1667264734158-c5710919-74af-4fb7-8fce-eb786151e39f.png" alt="Controller示意图" class="rounded">
                        <img src="https://cdn.nlark.com/yuque/0/2023/png/21449790/1689581945648-6eb988b4-3e2e-491a-99ca-61a8b9045c6e.png" alt="Controller流程图" class="rounded">
                    </div>
                    <div class="bg-blue-50 p-4 rounded border border-blue-100">
                        <h4 class="font-bold text-blue-700 mb-2 flex items-center">
                            <i class="fas fa-check-circle mr-2"></i>
                            适用场景
                        </h4>
                        <ul class="list-disc list-inside text-gray-700">
                            <li>传统Web应用</li>
                            <li>需要返回HTML页面的场景</li>
                            <li>前后端不分离的架构</li>
                        </ul>
                    </div>
                </div>

                <!-- @RestController Card -->
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-600 p-3 rounded-full mr-4">
                            <i class="fas fa-server text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold">@RestController</h3>
                    </div>
                    <p class="text-gray-700 mb-4">
                        <code>@RestController</code>是Spring 4.0引入的注解，结合了<code>@Controller</code>和<code>@ResponseBody</code>的功能，适用于构建RESTful API，返回数据而非视图。
                    </p>
                    <div class="code-block p-4 mb-4 text-sm overflow-x-auto">
                        <div class="lang">Java</div>
                        <pre><code>@RestController
public class ExampleRestController {
    @RequestMapping("/example")
    public ExampleObject handleRequest() {
        // 生成并返回响应数据
        return new ExampleObject();
    }
}</code></pre>
                    </div>
                    <div class="grid grid-cols-2 gap-2 mb-4">
                        <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1667264745311-1d7abc37-3e5d-44b5-8736-40864acf78f2.png" alt="RestController示意图" class="rounded">
                        <img src="https://cdn.nlark.com/yuque/0/2021/png/21449790/1635385488689-59bebab2-022d-46d6-acfc-3caf9e449902.png" alt="RestController流程图" class="rounded">
                    </div>
                    <div class="bg-purple-50 p-4 rounded border border-purple-100">
                        <h4 class="font-bold text-purple-700 mb-2 flex items-center">
                            <i class="fas fa-check-circle mr-2"></i>
                            适用场景
                        </h4>
                        <ul class="list-disc list-inside text-gray-700">
                            <li>RESTful Web服务</li>
                            <li>前后端分离架构</li>
                            <li>移动应用后端API</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Diagram Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 font-serif border-b pb-2">核心差异可视化</h2>
            <div class="diagram-container">
                <div class="mermaid">
                    graph TD
                    A[客户端请求] --> B{请求类型}
                    B -->|传统Web请求| C[@Controller]
                    B -->|API请求| D[@RestController]
                    C --> E[返回视图名称]
                    E --> F[视图解析器]
                    F --> G[渲染HTML]
                    D --> H[返回数据对象]
                    H --> I[自动转换为JSON/XML]
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="bg-white rounded-lg p-6 shadow-sm">
            <h2 class="text-3xl font-bold mb-6 font-serif">总结对比表</h2>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">特性</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">@Controller</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">@RestController</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">主要用途</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">传统Web应用，返回视图</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">RESTful API，返回数据</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">返回值处理</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">通过视图解析器解析为HTML</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">自动转换为JSON/XML</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">注解组合</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">单独使用</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">= @Controller + @ResponseBody</td>
                        </tr>
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">HTTP响应类型</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">通常为text/html</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">通常为application/json</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-4xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-sm">专业的技术知识分享平台</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        <span>访问语雀主页</span>
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <!-- Scripts -->
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>